package com.lx.util.secure.algorithm.summary;

import com.lx.util.LX;
import com.lx.util.secure.algorithm.SummaryAlgorithm;
import com.lx.util.secure.algorithm.coder.CoderType;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HmacSha extends SummaryAlgorithm<HmacSha> {

    //HmacSHA1,HmacSHA256,HmacSHA512;

    @Override
    protected CoderType defaultKeyCoderType() {
        return CoderType.BASE64;
    }

    @Override
    protected CoderType defaultCoderType() {
        return CoderType.BASE64;
    }

    @Override
    protected String defaultEncryptAlgorithm() {
        return "HmacSHA256";
    }

    @Override
    public byte[] encrypt(byte[] bytes) {
        return hmacSHA(getEncryptAlgorithm(),getKey(),bytes);
    }

    public byte[] encrypt(byte[] ... content) {
        return hmacSHA(getEncryptAlgorithm(),getKey(),content);
    }


    public static byte[] hmacSHA(String type, byte[] key,byte[] ... content){
        try {
            Mac mac = Mac.getInstance(type);
            mac.init(new SecretKeySpec(key, type));
            for (byte[] bytes : content) {
                mac.update(bytes);
            }
            return mac.doFinal();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
